﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMC.Prepare.Core;
using System.Web;
using IMC.Prepare.Web.Framework.Localization;
using IMC.Prepare.Services.Localization;
using IMC.Prepare.Core.Domain.Localization;
namespace IMC.Prepare.Web.Framework
{
    public partial class WebWorkContext : IWorkContext
    {
        private readonly ILanguageService _languageService;

        public WebWorkContext(ILanguageService languageService)
        {
            this._languageService = languageService;
        }
        /// <summary>
        /// Get or set current user working language
        /// </summary>
        public Language WorkingLanguage
        {
            get
            {
                ////get language from URL (if possible)
                //if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
                //{
                //    if (_httpContext != null)
                //    {
                //        string virtualPath = _httpContext.Request.AppRelativeCurrentExecutionFilePath;
                //        string applicationPath = _httpContext.Request.ApplicationPath;
                //        if (virtualPath.IsLocalizedUrl(applicationPath, false))
                //        {
                //            var seoCode = virtualPath.GetLanguageSeoCodeFromUrl(applicationPath, false);
                //            if (!String.IsNullOrEmpty(seoCode))
                //            {
                //                var langByCulture = _languageService.GetAllLanguages()
                //                    .Where(l => seoCode.Equals(l.UniqueSeoCode, StringComparison.InvariantCultureIgnoreCase))
                //                    .FirstOrDefault();
                //                if (langByCulture != null && langByCulture.Published)
                //                {
                //                    //the language is found. now we need to save it
                //                    if (this.CurrentCustomer != null &&
                //                        !langByCulture.Equals(this.CurrentCustomer.Language))
                //                    {
                //                        this.CurrentCustomer.Language = langByCulture;
                //                        _customerService.UpdateCustomer(this.CurrentCustomer);
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
                //if (this.CurrentCustomer != null &&
                //    this.CurrentCustomer.Language != null &&
                //    this.CurrentCustomer.Language.Published)
                //    return this.CurrentCustomer.Language;

                var lang = _languageService.GetAllLanguages().FirstOrDefault();
                return lang;
            }
            set
            {
                //if (this.CurrentCustomer == null)
                //    return;

                //this.CurrentCustomer.Language = value;
                //_customerService.UpdateCustomer(this.CurrentCustomer);
                return;
            }
        }




        #region IWorkContext Members


        public Core.Domain.Customers.Customer CurrentCustomer
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
